﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Enums;
using UIWindow.GlobalParameters;
using UIWindow.Helper;
using UIWindow.Lucifer;
using UIWindow.Services.Context;

namespace UIWindow.Services.Handle
{
    public class WeightClear : TaskEventBase<WeightClearContext>, ITaskEvent<WeightClearContext>
    {
        private DeviceGlobal _device;
        private ParameterGlobal _parameter;
        public WeightClear(DeviceGlobal device, ParameterGlobal parameter)
        {
            _device = device;
            _parameter = parameter;
        }
        public override Task Handle(WeightClearContext context)
        {
            $"称重清零执行".RunLog(MessageLevelType.Info);
            var list = _device.DeviceList.Where(x => x.Types == DeviceType.前称重);
            //var plc = _device.DeviceList.Where(x => x.Types == DeviceType.注液机).FirstOrDefault().Comm;
            foreach (var item in list)
            {
                item.Comm.Write(Encoding.UTF8.GetBytes("DZ\r\n"));
                item.Comm.ByteCache.Clear();
            }
            System.Threading.Thread.Sleep(2000);
            //if (plc.Write(new Models.S7NET.S7ParamModel() { Address = context.AddressResults, Value = new byte[] { 0x00, 0x01 } })) "称重清零--OKNG信号写入成功".RunLog(MessageLevelType.Info);
            //else "称重清零--OKNG信号写入失败".RunLog(MessageLevelType.Warning);
            return base.Handle(context);
        }
    }
}
